package com.cxhome.cxinfox.productinfo;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.jfinal.plugin.activerecord.Page;
import com.traffic.managent.base.BaseService;
import com.traffic.managent.base.Paginator;
import com.traffic.managent.model.product.ProductInfo;
import com.traffic.managent.util.SQLUtils;
import com.traffic.managent.util.UtilsUtils;

/**
 * 供应产品业务
 * 
 */
public class ProductInfoService extends BaseService {

	private final static Logger log = Logger.getLogger(ProductInfoService.class);
	public static final ProductInfoService service = new ProductInfoService();

	public Page<ProductInfo> paginate(ProductInfo model, Paginator paginate, StringBuilder orderSql) {

		SQLUtils sql = new SQLUtils(
				" from cx_product_info pi LEFT JOIN cx_provider cp ON pi.providerId = cp.ids  where 1 = 1  ");

		if (model.getAttrValues().length != 0) {
			sql.whereEquals("pi.productStatus", model.getStr("productStatus"));
			sql.whereEquals("pi.region", model.getStr("region"));
			sql.whereLike("pi.productName", model.getStr("productName"));
			sql.whereEquals("pi.providerId", model.getStr("providerId"));
		}

		sql.append(orderSql);
		return ProductInfo.dao.paginate(paginate, "select pi.*,cp.providerName ", sql.toString());

	}

	public void saveOrUpdate(String ids, ProductInfo model, String userid) {
		model.put("update_id", userid);
		model.put("update_time", new Date());

		if (ids != null && !"".equals(ids)) { // 更新
			// model.set("modifyBy", getSessionUser().getUseriD());
			model.set("updateTime", new Date());
			model.update();
		} else { // 新增
			model.put("ids", UtilsUtils.getOrderId(30, "pi"));
			model.put("createName", userid);
			model.put("createTime", new Date());
			model.save();
		}

	}

	/**
	 * 条件查询供应商产品
	 */

	public List<ProductInfo> findByPara(String productName) {
		String sqlinfo = "select * from cx_product_info cpi where 1=1 ";
		if (productName != null) {
			sqlinfo += " and cpi.productName like '%" + productName.trim() + "%'";
		}
		return ProductInfo.dao.find(sqlinfo);
	}
}
